/*
 * Author : Thanh Pham
 * Create : 13:54 , 24/9/3013
 * 
 */

package Bussiness;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LogoutAction extends ActionSupport{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession session;
    
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        try{
            request = ServletActionContext.getRequest();
            response = ServletActionContext.getResponse();
            session = request.getSession();
            session.removeAttribute("userSession");
            Cookie [] cookies = request.getCookies();
            Cookie cookieTemp = null;
            int n = cookies.length;
            
            for(int i=0;i<n;i++){
                if(cookies[i].getName().equals("c_u")){
                    cookieTemp = cookies[i];
                    cookieTemp.setMaxAge(0);
                    response.addCookie(cookieTemp);
                }
            }
                        
            System.out.println(" Ok da xoa cookies. cookiesTemp: "+cookieTemp);
        }catch(Exception ex){
            System.out.println(" Ngoai le xay ra khi logout !");
        }
        return SUCCESS;
    }
}
